//
//  RxOptional.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2019/1/22.
//  Copyright © 2019 Mr.wang. All rights reserved.
//     __            __            __
//     \ \          /  \          / /     帅™
//      \ \        / /\ \        / /
//       \ \      / /  \ \      / /
//        \ \    / /    \ \    / /
//         \ \  / /      \ \  / /
//          \ \/ /        \ \/ /
//           \__/          \__/


import UIKit
import RxSwift

public protocol OptionalType {
    
    associatedtype Wrapped
    
    var value: Wrapped? { get }
    
}

extension Optional: OptionalType {
    
    public var value: Wrapped? {
        return self
    }
}

public extension ObservableType where Element: OptionalType {
    
    func filterNil() -> Observable<Element.Wrapped> {
        return self.flatMap { element -> Observable<Element.Wrapped> in
            guard let value = element.value else {
                return Observable<Element.Wrapped>.empty()
            }
            return Observable<Element.Wrapped>.just(value)
        }
    }
}

public extension ObservableType {
    
    func mapOptional() -> Observable<Optional<Element>> {
        return self.map { Optional.some($0) }
    }
    
}
